package manageFriendshipsServlets;

import java.io.IOException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sessions.friendshipSession.FriendshipSessionRemote;

public class EraseFriendshipServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		eraseFriendship(request, response);
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		eraseFriendship(request, response);
	}

	private void eraseFriendship(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Context jndiCtx;
		int myID, idReceiver;
		myID = (Integer)request.getSession().getAttribute("id");
		idReceiver = Integer.parseInt(request.getParameter("idReceiver"));
		try {
			jndiCtx = new InitialContext();
			FriendshipSessionRemote friendshipSession = (FriendshipSessionRemote) jndiCtx
					.lookup("Progetto/FriendshipSession/remote");
			if (friendshipSession.eraseFriendship(myID,idReceiver))
				//utente eliminato
				request.getSession().setAttribute("erased", "true");
			else
				//utente non eliminato
				request.getSession().setAttribute("erased", "false");
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("Errore nella servlet");
		}
		response.sendRedirect("/ProgettoWeb/ResearchedUser.html?id=" + idReceiver);
	}

}
